<template>
  <el-dialog
    width="70%"
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="hotel-protocol"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="问题编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="问题编码"></el-input>
      </el-form-item>
      <el-form-item label="问题标题" prop="name">
        <el-input v-model="dataForm.name" placeholder="问题标题"></el-input>
      </el-form-item>
      <el-form-item label="问题内容" prop="content">
        <quill-editor
          style="height:400px"
          class="editor"
          v-model="dataForm.content"
          ref="myQuillEditor"
          :options="editorOption"
          @blur="onEditorBlur($event)"
          @focus="onEditorFocus($event)"
          @change="onEditorChange($event)"
        ></quill-editor>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        content: "",
        code: ""
      },
      dataRule: {
        name: [
          { required: true, message: "问题标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "问题内容不能为空", trigger: "blur" }
        ],
        code: [{ required: true, validator: this.codeRE, trigger: "blur" }]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      console.log(this.dataForm);
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelproblem/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.hotelproblem.name;
              this.dataForm.content = data.hotelproblem.content;
            }
          });
        }
      });
    },
    // code纯数字校验
    codeRE(rule, value, callback) {
      const reg = /^[0-9]*$/;
      if (!value) {
        return callback(new Error("问题编码不能为空"));
      } else if (! reg.test(value)) {
        return callback(new Error("问题编码必须是数字"));
      } else {
        callback();
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelproblem/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              content: this.dataForm.content,
              code:this.dataForm.code
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    onEditorBlur() {},
    onEditorFocus() {
      //获得焦点事件
    },
    onEditorChange() {
      //内容改变事件
    }
  }
};
</script>
<style lang="less">
.hotel-protocol {
  .el-dialog__body {
    height: 80vh;
  }
}
</style>

